package com.gsafety.library.Utils;
import com.tencent.mmkv.MMKV;

/**
 * @author : lj
 * @date : 2020/12/4 16:04
 */
public class SpUtil {
    private static SpUtil spUtil;
    private MMKV mmkv;

    public static SpUtil getInstance() {
        if (spUtil == null) {
            spUtil = new SpUtil();
        }
        return spUtil;
    }

    private SpUtil() {
        mmkv = MMKV.defaultMMKV();
    }

    /**
     * 加入缓存
     *
     * @param key
     * @param value
     */
    public void encode(String key, Object value) {
        if (value instanceof String) {
            mmkv.encode(key, (String) value);
        } else if (value instanceof Float) {
            mmkv.encode(key, (Float) value);
        } else if (value instanceof Boolean) {
            mmkv.encode(key, (Boolean) value);
        } else if (value instanceof Integer) {
            mmkv.encode(key, (Integer) value);
        } else if (value instanceof Long) {
            mmkv.encode(key, (Long) value);
        } else if (value instanceof Double) {
            mmkv.encode(key, (Double) value);
        }
    }

    public int decodeInt(String key) {
        return mmkv.decodeInt(key, 0);
    }

    public Double decodeDouble(String key) {
        return mmkv.decodeDouble(key, 0.00);
    }

    public Long decodeLong(String key) {
        return mmkv.decodeLong(key, 0L);
    }

    public Boolean decodeBoolean(String key) {
        return mmkv.decodeBool(key, false);
    }

    public Float decodeFloat(String key) {
        return mmkv.decodeFloat(key, 0F);
    }


    public String decodeString(String key) {
        return mmkv.decodeString(key, "");

    }

    public void removeKey(String key) {
        mmkv.removeValueForKey(key);
    }

    public void clearAll() {
        mmkv.clearAll();
    }
}
